package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PictureDao;
import com.itheima.dao.PictureSortDao;
import com.itheima.pojo.Picture;
import com.itheima.pojo.PictureSort;
import com.itheima.service.PictureSortService;
import com.itheima.utils.DateUtils;
import com.itheima.utils.IdWorker;
import com.itheima.vo.PictureSortVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author kehaonan
 * @version 1.0
 * @date 2020/7/7 11:10
 */
@Service
public class PictureSortServiceImpl implements PictureSortService {
    @Autowired
    private PictureSortDao pictureSortDao;

    @Autowired
    private PictureDao pictureDao;

    @Autowired
    public IdWorker idWorker;

    @Override
    public void add(PictureSortVO pictureSortVO) {
        PictureSort pictureSort = new PictureSort();
        pictureSort.setUid(String.valueOf(new IdWorker().nextId()));
        pictureSort.setFileUid(pictureSortVO.getFileUid());
        pictureSort.setName(pictureSortVO.getName());
        pictureSort.setStatus(pictureSortVO.getStatus());
        pictureSort.setCreateTime(DateUtils.getNowTime());
        pictureSort.setUpdateTime(DateUtils.getNowTime());
        pictureSort.setParentUid(pictureSortVO.getParentUid());
        pictureSort.setSort(pictureSortVO.getSort());
        pictureSort.setIsShow(pictureSortVO.getIsShow());
        pictureSortDao.insert(pictureSort);
    }

    @Override
    public void delete(PictureSortVO pictureSortVO) {
        Picture picture = new Picture();
        picture.setPictureSortUid(pictureSortVO.getUid());
        //先判断分类是否包含图片,包含就提示用户,不能删除
        List<Picture> list = pictureDao.select(picture);

        if (list.size() == 0){
            //说用包含图片
            throw new RuntimeException("图片分类中包含图片,不可以直接删除");
        }

        PictureSort pictureSort = new PictureSort();
        pictureSort.setUid(pictureSortVO.getUid());
        //删除做逻辑删除,设置状态为0
        pictureSortVO.setStatus(0);
        //修改状态就相当于删除
        edit(pictureSortVO);
    }



    @Override
    public Page<PictureSort> getList(PictureSortVO pictureSortVO) {
        //当前页
        Integer currentPage = pictureSortVO.getCurrentPage();
        //每页数据
        Integer pageSize = pictureSortVO.getPageSize();
        //分页查询
        PageHelper.startPage(currentPage, pageSize);

        //查询条件构造
        Example example=new Example(PictureSort.class);
        Example.Criteria criteria = example.createCriteria();
        //状态为1的才可以被查询到
        criteria.andEqualTo("status", 1);
        // 根据图片分类name进行查询
        if(pictureSortVO.getName()!=null && !"".equals(pictureSortVO.getName())){
            criteria.andLike("name", "%"+pictureSortVO.getName()+"%");
        }
        //查询所有的时候按权重进行排序
        example.orderBy("sort").desc();
        return (Page<PictureSort>) pictureSortDao.selectByExample(example);
    }

    @Override
    public void edit(PictureSortVO pictureSortVO) {
        PictureSort pictureSort = new PictureSort();
        pictureSort.setUid(pictureSortVO.getUid());
        pictureSort.setFileUid(pictureSortVO.getFileUid());
        pictureSort.setName(pictureSortVO.getName());
        pictureSort.setStatus(pictureSortVO.getStatus());
        pictureSort.setUpdateTime(DateUtils.getNowTime());
        pictureSort.setParentUid(pictureSortVO.getParentUid());
        pictureSort.setSort(pictureSortVO.getSort());
        pictureSort.setIsShow(pictureSortVO.getIsShow());
        pictureSortDao.updateByPrimaryKeySelective(pictureSort);
    }


    @Override
    public PictureSort getPictureSortByUid(PictureSortVO pictureSortVO) {
        if (pictureSortVO.getStatus()==0){
            throw new RuntimeException("图片状态为已删除,不可显示");
        }

        return pictureSortDao.selectByPrimaryKey(pictureSortVO.getUid());
    }

    @Override
    public List<PictureSort> stick(PictureSortVO pictureSortVO) {


        PictureSort pictureSort = new PictureSort();
        pictureSort.setUid(pictureSortVO.getUid());
        //置顶设置权重为101
        pictureSort.setSort(101);
        pictureSort.setUpdateTime(DateUtils.getNowTime());
        //修改状态就相当于删除
        pictureSortDao.updateByPrimaryKeySelective(pictureSort);

        return getList(pictureSortVO);
    }
}
